package com.sevenstudio.adu.gasing.object;

import java.io.IOException;
import java.io.InputStream;

import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.util.adt.io.in.IInputStreamOpener;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.sevenstudio.adu.gasing.define.iGameDefines;
import com.sevenstudio.adu.gasing.element.ASprite;
import com.sevenstudio.adu.gasing.engine.GameEngine;
import com.sevenstudio.adu.gasing.engine.Utils;

public class Gasing
{
	GameEngine		engine;
	GasingElement gasing;
	Body			body;
	PhysicsWorld	world;
	PhysicsConnector connector;
	Sprite			sprite, shadow;
	ASprite[] item_effect = new ASprite[iGameDefines.SPRITE_EFFECT_ITEM.length];
	boolean play;
	float putaran, kecepatan, benturanX, benturanY;
	
	
	public Gasing(GameEngine gameEngine, GasingElement[] arrayGasing,
			int index, PhysicsWorld world)
	{
		this.engine = gameEngine;
		this.world = world;
		this.kecepatan = arrayGasing[index].getKecepatan();
		this.putaran = arrayGasing[index].getPutaran() * 10;
		this.gasing = arrayGasing[index];
		

		Bitmap bitmap;
		BitmapTextureAtlas bbta = null;
		try {
			bitmap = BitmapFactory.decodeStream(engine.getAssets().open(arrayGasing[index].getPath()));
			System.out.println(bitmap.getWidth());
			System.out.println( Utils.GetBigTwoInt(bitmap.getWidth()));
			bbta = new BitmapTextureAtlas(engine.getTextureManager(), Utils.GetBigTwoInt(bitmap.getWidth()), Utils.GetBigTwoInt(bitmap.getHeight()), TextureOptions.REPEATING_BILINEAR);
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
			TextureRegion ttr = BitmapTextureAtlasTextureRegionFactory.createFromAsset(bbta, engine.getAssets(), arrayGasing[index].getPath(), 0, 0);


		bbta.load();
			
		sprite = new Sprite(Utils.GetAnchorX(Utils.GetRatioWidth(arrayGasing[index].getW()), arrayGasing[index].getAnchor()) + Utils.GetRatioWidth(arrayGasing[index].getX()),
				Utils.GetAnchorY(Utils.GetRatioWidth(arrayGasing[index].getH()), arrayGasing[index].getAnchor()) + Utils.GetRatioWidth(arrayGasing[index].getY()),
				Utils.GetRatioWidth(arrayGasing[index].getW()),
				Utils.GetRatioWidth(arrayGasing[index].getH()),
				ttr,
				engine.getVertexBufferObjectManager());
		
		final String pathShadow = arrayGasing[index].getShadow().getPath();
		try
		{
			ITexture gameSpriteITexture = new BitmapTexture(engine.getTextureManager(), new IInputStreamOpener()
					{
						public InputStream open() throws IOException
						{
							return engine.getAssets().open(pathShadow);
						}
					});
			gameSpriteITexture.load();
			ITextureRegion gameSpriteTextureRegion = TextureRegionFactory
					.extractFromTexture(gameSpriteITexture);
			
			shadow = new Sprite(Utils.GetAnchorX(Utils.GetRatioWidth(arrayGasing[index].getShadow().getW()), arrayGasing[index].getShadow().getAnchor()) + Utils.GetRatioWidth(arrayGasing[index].getShadow().getX()),
					Utils.GetAnchorY(Utils.GetRatioWidth(arrayGasing[index].getShadow().getH()), arrayGasing[index].getShadow().getAnchor()) + Utils.GetRatioWidth(arrayGasing[index].getShadow().getY()),
					Utils.GetRatio(arrayGasing[index].getShadow().getW()),
					Utils.GetRatio(arrayGasing[index].getShadow().getH()),
					gameSpriteTextureRegion.deepCopy(),
					engine.getVertexBufferObjectManager());
			
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		
		body = PhysicsFactory.createCircleBody(world, sprite,
				BodyType.DynamicBody,
				PhysicsFactory.createFixtureDef(arrayGasing[index].getBerat() / 10, 0.5f, 1f));
		body.setUserData("mc");
		connector = new PhysicsConnector(sprite, body);
		world.registerPhysicsConnector(connector);
		
		for(int i =0; i < iGameDefines.SPRITE_EFFECT_ITEM.length; i++)
		{
			item_effect[i] = new ASprite(engine, iGameDefines.SPRITE_EFFECT_ITEM, i);
			item_effect[i].SetPos(Utils.getCenterObjX(sprite, item_effect[i].theSprite()), Utils.getCenterObjY(sprite, item_effect[i].theSprite()));
		}
		
		shadow.setPosition(sprite.getX() + sprite.getWidth() / 2, sprite.getY() + sprite.getHeight() / 2);
	}
	
	public Body getBody()
	{
		return body;
	}
	
	public Sprite getSprite()
	{
		return sprite;
	}
	
	public void setPlay(boolean play)
	{
		this.play = play;
	}
	
	public boolean isPlay()
	{
		return play;
	}
	
	public Sprite getShadow()
	{
		return shadow;
	}
	
	public Sprite getItemEffect(int index)
	{
		return item_effect[index].theSprite();
	}
			
	public float getCenterX()
	{
		return sprite.getX() + sprite.getWidth() / 2;
	}
	
	public float getCenterY()
	{
		return sprite.getY() + sprite.getHeight() / 2;
	}
	
	public void perlambat(float bar)
	{
		putaran = bar / GameEngine.CAMERA_HEIGHT * 75 + 25;
	}
	
	public void benturan()
	{
		benturanX = body.getPosition().x * PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT;
		benturanY = body.getPosition().y * PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT;
	}
	
	public float getBenturanX()
	{
		return benturanX;
	}
	public float getBenturanY()
	{
		return benturanY;
	}
	
	protected float getSpeedX(float x, float y)
	{
		float miring = (float) Math.sqrt((float) Math.pow(x, 2)
				+ (float) Math.pow(y, 2));
		
		return x / miring * kecepatan;
	}
	
	protected float getSpeedY(float x, float y)
	{
		float miring = (float) Math.sqrt((float) Math.pow(x, 2)
				+ (float) Math.pow(y, 2));
		
		return y / miring * kecepatan;
	}
	
	public void reset()
	{
		putaran = gasing.getPutaran() * 10;
		body.setLinearVelocity(0, 0);
		play = false;
	}
	
	public void destroyBody()
	{
		world.unregisterPhysicsConnector(connector);
		world.destroyBody(body);
	}

	public void onUpdate()
	{
		body.setAngularVelocity(putaran);
		shadow.setPosition(sprite.getX() + sprite.getWidth() / 2, sprite.getY() + sprite.getHeight() / 2);
	}
	
	
}
